﻿using System;
using System.Collections.Generic;

// 抽象类Shape
public abstract class Shape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

// 长方形类
public class Rectangle : Shape
{
    public double Length { get; private set; }
    public double Width { get; private set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
        Console.WriteLine($"Rectangle: Length={Length}, Width={Width},Area={CalculateArea()}");
    }

    public override double CalculateArea() => Length * Width;
    public override bool IsValid() => Length > 0 && Width > 0;
}

// 正方形类
public class Square : Shape
{
    public double Side { get; private set; }

    public Square(double side)
    {
        Side = side;
        Console.WriteLine($"Square: Side={Side},Area={CalculateArea()}");
    }

    public override double CalculateArea() => Side * Side;
    public override bool IsValid() => Side > 0;
}

// 三角形类
public class Triangle : Shape
{
    public double A { get; private set; }
    public double B { get; private set; }
    public double C { get; private set; }

    public Triangle(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
        Console.WriteLine($"Triangle: A={A}, B={B}, C={C},Area={CalculateArea()}");
    }

    //这是海伦公式
    public override double CalculateArea()
    {
        double s = (A + B + C) / 2;
        return Math.Sqrt(s * (s - A) * (s - B) * (s - C));
    }

    public override bool IsValid()
    {
        return A > 0 && B > 0 && C > 0
            && A + B > C
            && A + C > B
            && B + C > A;
    }
}

// 简单工厂类:随机生成形状
public class ShapeFactory
{
    private readonly Random _random = new Random();

    public Shape CreateShape()
    {
        int type = _random.Next(0, 3); // 随机选择形状类型

        switch (type)
        {
            case 0:
                return CreateRectangle();
            case 1:
                return CreateSquare();
            case 2:
                return CreateTriangle();
            default:
                throw new ArgumentException("Invalid shape type");
        }
    }

    private Shape CreateRectangle()
    {
        return new Rectangle(
            _random.NextDouble() * 10 + 1,
            _random.NextDouble() * 10 + 1
        );
    }

    private Shape CreateSquare()
    {
        return new Square(_random.NextDouble() * 10 + 1);
    }

    private Shape CreateTriangle()
    {
        double a = _random.NextDouble() * 10 + 1;
        double b = _random.NextDouble() * 10 + 1;

        //这里一定要先算出最小边和最大边!!!!!确保合法!!!!
        double minC = Math.Max(a, b);
        double maxC = a + b;
        double c = _random.NextDouble() * (maxC - minC) + minC;

        return new Triangle(a, b, c);
    }
}

class Program
{
    static void Main()
    {
        ShapeFactory factory = new ShapeFactory();
        List<Shape> shapes = new List<Shape>();
        double totalArea = 0;

        for (int i = 0; i < 10; i++)
        {
            Shape shape = factory.CreateShape();
            shapes.Add(shape);
            totalArea += shape.CalculateArea();
        }

        Console.WriteLine($"Total area of 10 shapes: {totalArea:N2}");
    }
}
